﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.Maiyoule.GsmSms {
    class GsmGroup {
        public GsmGroup() {
        }
        public GsmGroup(string name) {
            this.name = name;
        }

        private string name;
        /// <summary>
        /// 名称
        /// </summary>
        public string Name {
            get { return name; }
            set { name = value; }
        }
        private GsmContral contral;
        /// <summary>
        /// GSM控制器
        /// </summary>
        internal GsmContral Contral {
            get { return contral; }
            set { contral = value; }
        }


        private int index;
        /// <summary>
        /// 内部索引
        /// </summary>
        public int Index {
            get { return index; }
            set { index = value; }
        }
        /// <summary>
        /// 释放
        /// </summary>
        public void DisposeAll() {
            if (contral != null) {
                contral.Close();
            }
        }


        private IEvent _callback;

        public IEvent Callback {
            get { return _callback; }
            set {
                _callback = value;
                if (contral != null) {
                    contral.SetCallBack(_callback);
                }
            }
        }

        /// <summary>
        /// 打开短信猫
        /// </summary>
        public void Open() {
            OpenGsm opengsm = new OpenGsm(this._Open);
            opengsm.BeginInvoke(delegate(IAsyncResult result) {
                bool openresult = opengsm.EndInvoke(result);
                if(openresult){
                    //调整时间
                    //调整短信猫时间
                    DateTime modetime = this.contral.GetModeCurTime();
                    if (Math.Abs(modetime.Ticks - DateTime.Now.Ticks) > 10000) {
                        //大于10秒 修正时间
                        this.contral.SetModeCurTime(DateTime.Now);
                    }

                }
                if (_callback != null) {
                    if (openresult) {
                        _callback.OnOpenSuccess();
                    } else {
                        _callback.OnOpenFaild();
                    }
                }
            }, null);
        }
        //打开短信猫 异步
        private bool _Open() {
            if (_callback != null) {
                _callback.OnStartOpen();
            }
            return this.contral.Open();
        }


        public void Close() {
        }
        private void _Close() {
            
        }


    }
}
